import java.io.IOException;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.filter.ElementFilter;
import org.xml.sax.SAXException;

import net.juniper.netconf.Device;
import net.juniper.netconf.NetconfException;
import net.juniper.netconf.XML;
import net.juniper.netconf.XMLBuilder;


/**
 * Methos collection to deal with Netconfig communications
 * @author xhagesta
 *
 */
public class netconfInterface {
	public XMLBuilder buildXML = null;
	public XML configurationXML = null;
	public XML configXML = null;
	public XML profileXML = null;
	public XML poolXML = null;
	public XML firewallXML = null;
	private final String POOLNAME = "srxpool";
	private final String PROFILENAME = "local";
	private final String RANGENAME = "dvpnrange";

	/**
	 * Constructor
	 * @throws ParserConfigurationException
	 */
	public netconfInterface() throws ParserConfigurationException {
		buildXML = new XMLBuilder();
		configurationXML = buildXML.createNewRPC("edit-config").append("config").append("configuration");
		configXML = configurationXML.append("access");
		profileXML = configXML.append("profile");
		profileXML.setAttribute("operation", "replace");
		profileXML.append("name", PROFILENAME);
		profileXML.append("authentication-order","password");
		profileXML.append("address-assignment").append("pool",POOLNAME);
		poolXML = configXML.append("address-assignment").append("pool");
		poolXML.setAttribute("operation", "replace");
		firewallXML = configXML.append("firewall-authentication");
		firewallXML.setAttribute("operation", "replace");
		firewallXML.append("web-authentication").append("default-profile").setText(PROFILENAME);
	}
	
	/**
	 * This method sends the configuration to the device and then commits it
	 * @param network		Network definition for remote Dynamic VPN users
	 * @param lowIp			Low IP address in range for remote Dynamic VPN users
	 * @param highIp		High IP address in range for remote Dynamic VPN users
	 * @param dnsIp			DNS address to be installed in the remote user's workstation
	 * @param document		JDOM Document holding all user/password database
	 * @param srxDevice		Netconfig device object for the SRX device to be updated
	 * @throws NetconfException
	 * @throws SAXException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void commitConfig (String network,
			                  String lowIp,
			                  String highIp,
			                  String dnsIp,
			                  Document document,
			                  Device srxDevice ) throws NetconfException, SAXException, IOException{
		poolXML.append("name", POOLNAME);
		XML family = poolXML.append("family").append("inet");
		family.append("network", network);
		family.append("xauth-attributes").append("primary-dns", dnsIp);
		XML range = family.append("range");
		range.append("name", RANGENAME);
		range.append("low",lowIp);
		range.append("high",highIp);
		Iterator<Element> itr = document.getDescendants(new ElementFilter("client"));
	    while (itr.hasNext()) {
	    	Element clientData = itr.next();
	    	XML client = profileXML.append("client");
	    	client.append("name", clientData.getChildText("name"));
	    	client.append("firewall-user").append("password",clientData.getChild("firewall-user").getChildText("password"));
	    	}
	    srxDevice.executeRPC(configXML);
	    srxDevice.commit();	    
	}

}
